import {
  getoffsales,
  getoffhire,
  getshopsales,
  getshophire,
} from '../api/shop.ts';
const IndexModel = {
  namespace: 'offslaes',

  state: {
    offsldata: [],
    offhiredata: [],
    shopsalesdata: [],
    shophiredata: [],
  },

  effects: {
    // 写字楼出售
    *GETOFFSALES({ payload }, { call, put }) {
      const data = yield getoffsales();
      // console.log(data,1111)
      yield put({ type: 'SETOFFSALES', payload: data.data });
    },
    // 写字楼出租
    *GETOFFHIRE({ payload }, { call, put }) {
      const data = yield getoffhire();
      // console.log(data,2222)
      yield put({ type: 'SETOFFHIRE', payload: data.data });
    },
    // 商铺出租
    *GETSHOPSALES({ payload }, { call, put }) {
      const data = yield getshopsales();
      // console.log(data,3333)
      yield put({ type: 'SETSHOPSALES', payload: data.data });
    },
    // 商铺出租
    *GETSHOPHIRE({ payload }, { call, put }) {
      const data = yield getshophire();
      // console.log(data,444)
      yield put({ type: 'SETSHOPHIRE', payload: data.data });
    },
  },
  reducers: {
    // 写字楼出售
    SETOFFSALES(state, { payload }) {
      state.offsldata = payload;
      return {
        ...state,
      };
    },
    // 修改
    EDIESETOFFSALES(state, { payload }) {
      console.log(payload);
      const { title, id } = payload;
      const newState = JSON.parse(JSON.stringify(state));
      const index = newState.offsldata.findIndex((item) => {
        return item.id === id;
      });
      console.log(index);
      newState.offsldata[index].title = title;
      return {
        ...newState,
      };
    },

    // 添加写字楼出售数据
    ADD_OFFICESELL(state, { payload }) {
      const { title, price, estate_name } = payload;
      // console.log(state)
      const newState = JSON.parse(JSON.stringify(state));
      newState.offsldata.push({
        id: newState.offsldata[newState.offsldata.length - 1].id + 1,
        title: title,
        // estate_name:title,
        price: price,
        lng: '110.30991946701',
        lat: '20.021263625269',
        ordid: 1000,
        update_time: new Date().getTime(),
        room_num: null,
        sale_status: '30',
        status: 1,
      });
      console.log(newState, '1111');
      return {
        ...newState,
      };
    },
    OFFSALESSEARCH(state, { payload }) {
      const offsalesarr = state.offsldata.filter((item) => {
        return item.title.includes(payload);
      });
      return {
        ...state,
        offsldata: offsalesarr,
      };
    },
    DELOFFSALES(state, { payload }) {
      // console.log(state)
      // console.log(payload)
      const newState = JSON.parse(JSON.stringify(state));
      newState.offsldata.splice(payload, 1);
      return {
        ...newState,
      };
    },
    // 写字楼出租
    SETOFFHIRE(state, { payload }) {
      state.offhiredata = payload;
      return {
        ...state,
      };
    },
    // 修改
    EDIESETOFFHIRE(state, { payload }) {
      console.log(payload);
      const { title, id } = payload;
      const newState = JSON.parse(JSON.stringify(state));
      const index = newState.offhiredata.findIndex((item) => {
        return item.id === id;
      });
      console.log(index);
      newState.offhiredata[index].title = title;
      return {
        ...newState,
      };
    },
    // 添加写字楼出租数据
    ADD_OFFICElET(state, { payload }) {
      const { price, title } = payload;
      const newState = JSON.parse(JSON.stringify(state));
      newState.offhiredata.push({
        id: newState.offhiredata[newState.offhiredata.length - 1].id + 1,
        title: title,
        price: price,
        title: title,
        lng: '110.30991946701',
        lat: '20.021263625269',
        ordid: 100,
        update_time: new Date().getTime(),
        room_num: null,
        sale_status: '30',
        status: 1,
      });
      console.log(newState.offhiredata, 'newState.officeLetList333333');
      return {
        ...newState,
      };
    },
    DELOFFHIRE(state, { payload }) {
      const newState = JSON.parse(JSON.stringify(state));
      newState.offhiredata.splice(payload, 1);
      return {
        ...newState,
      };
    },
    OFFHIRESEARCH(state, { payload }) {
      const offsalesarr = state.offhiredata.filter((item) => {
        return item.title.includes(payload);
      });
      return {
        ...state,
        offhiredata: offsalesarr,
      };
    },
    // 商铺出售
    SETSHOPSALES(state, { payload }) {
      state.shopsalesdata = payload;
      return {
        ...state,
      };
    },
    // 修改
    EDIESETSHOPSALES(state, { payload }) {
      console.log(payload);
      const { title, id } = payload;
      const newState = JSON.parse(JSON.stringify(state));
      const index = newState.shopsalesdata.findIndex((item) => {
        return item.id === id;
      });
      console.log(index);
      newState.shopsalesdata[index].title = title;
      return {
        ...newState,
      };
    },
    // 添加商铺出售数据
    ADD_SHOPSELL(state, { payload }) {
      const { price, title } = payload;
      const newState = JSON.parse(JSON.stringify(state));
      newState.shopSellList.push({
        id: newState.shopsalesdata[newState.shopsalesdata.length - 1].id + 1,
        title: title,
        price: price,
        title: title,
        lng: '110.30991946701',
        lat: '20.021263625269',
        ordid: 100,
        update_time: new Date().getTime(),
        room_num: null,
        sale_status: '30',
        status: 1,
      });
      console.log(newState.shopsalesdata, 'newState.shopSellList333333');
      return {
        ...newState,
      };
    },
    SHOPSALESSEARCH(state, { payload }) {
      const offsalesarr = state.shopsalesdata.filter((item) => {
        return item.title.includes(payload);
      });
      return {
        ...state,
        shopsalesdata: offsalesarr,
      };
    },
    DELOSHOPSALES(state, { payload }) {
      const newState = JSON.parse(JSON.stringify(state));
      newState.shopsalesdata.splice(payload, 1);
      return {
        ...newState,
      };
    },
    // 商铺出租
    SETSHOPHIRE(state, { payload }) {
      state.shophiredata = payload;
      return {
        ...state,
      };
    },
    // 修改
    EDIESETSHOPHIRE(state, { payload }) {
      console.log(payload);
      const { title, id } = payload;
      const newState = JSON.parse(JSON.stringify(state));
      const index = newState.shophiredata.findIndex((item) => {
        return item.id === id;
      });
      console.log(index);
      newState.shophiredata[index].title = title;
      return {
        ...newState,
      };
    },
    // 添加商铺出租数据
    ADD_SHOPlET(state, { payload }) {
      const { price, title } = payload;
      const newState = JSON.parse(JSON.stringify(state));
      newState.shophiredata.push({
        id: newState.shophiredata[newState.shopLetList.length - 1].id + 1,
        title: title,
        price: price,
        title: title,
        lng: '110.30991946701',
        lat: '20.021263625269',
        ordid: 100,
        update_time: new Date().getTime(),
        room_num: null,
        sale_status: '30',
        status: 1,
      });
      console.log(newState.shophiredata, 'newState.shopLetList333333');
      return {
        ...newState,
      };
    },
    SHOPHIRESEARCH(state, { payload }) {
      const offsalesarr = state.shophiredata.filter((item) => {
        return item.title.includes(payload);
      });
      return {
        ...state,
        shophiredata: offsalesarr,
      };
    },
    DELOSHOPHIRE(state, { payload }) {
      const newState = JSON.parse(JSON.stringify(state));
      newState.shophiredata.splice(payload, 1);
      return {
        ...newState,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },
  // 自执行
  subscriptions: {
    AAA({ dispatch }) {
      dispatch({ type: 'GETOFFSALES' });
    },
    BBB({ dispatch }) {
      dispatch({ type: 'GETOFFHIRE' });
    },
    CCC({ dispatch }) {
      dispatch({ type: 'GETSHOPSALES' });
    },
    DDD({ dispatch }) {
      dispatch({ type: 'GETSHOPHIRE' });
    },
  },
};

export default IndexModel;
